﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WTEvection
{
    public partial class SelEmpfrm : Form
    {
        DataTable m_detail = null;
        DataTable m_dt = null;

        public SelEmpfrm( DataTable detail)
        {
            InitializeComponent();
            m_detail = detail;
          //  this.dgv.CellFormatting += new DataGridViewCellFormattingEventHandler(dgv_CellFormatting);
          //  this.dgv.CellValueChanged += new DataGridViewCellEventHandler(dgv_CellValueChanged);
            InitForm();
            LoadData();
        }

        //void dgv_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        //{
        //    if (e.RowIndex >= 0 )
        //    {
        //        DataGridViewRow drv = this.dgv.Rows[e.RowIndex];
        //        DataRow dr = (drv.DataBoundItem as DataRowView).Row;
        //        Boolean bs = Convert.ToBoolean(dr["Select"] == System.DBNull.Value ? false  : dr["Select"]);
        //        if (bs==true )
        //        {
        //             drv.DefaultCellStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFD0D0");
        //            drv.Cells[e.ColumnIndex].Style.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFD0D0");
        //        }
        //        else
        //        {
        //            drv.DefaultCellStyle.BackColor = System.Drawing.SystemColors.Window;
        //            drv.Cells[e.ColumnIndex].Style.BackColor = System.Drawing.SystemColors.Info;
        //        }
        //    }
        //}

        //void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        //{
        //    if (e.ColumnIndex == 0)
        //    {
        //        e.Value = e.RowIndex + 1;
        //    }
        //}

        void InitForm()
        {
            this.ckbAll.Click += new EventHandler(ckbAll_Click);
            this.cmbItemType.TextChanged += new EventHandler(cmbPubMNo_TextChanged);
           // this.txtZJM.TextChanged += new EventHandler(cmbPubMNo_TextChanged);
            this.txtEmpName.TextChanged += new EventHandler(cmbPubMNo_TextChanged);

            this.dgv.AutoGenerateColumns = false;
          
        }

        void LoadData()
        {
            string SqlSelect = @"
                SELECT Employees.EmployeeID, Employees.EmpName, Employees.DepartmentId,Department.DepartmentName
           FROM Employees LEFT OUTER JOIN
                 Department ON Employees.DepartmentId = Department.DepartmentId
                WHERE (Employees.EmpStatus = 1)
            ";
            Srvtools.InfoDataSet ids = new Srvtools.InfoDataSet();
            ids.Active = false;
            ids.AlwaysClose = true;
            ids.DataCompressed = true;
            ids.RemoteName = "GLModule.cmdRefValUse";
            ids.PacketRecords = -1;
            ids.CommandText = SqlSelect;
            ids.SetWhere("");
            this.m_dt = ids.RealDataSet.Tables[0];
            this.m_dt.CaseSensitive = false;

            DataColumn _dc;
            _dc = new DataColumn("Select", typeof(bool)); _dc.AllowDBNull = false; _dc.DefaultValue = false; m_dt.Columns.Add(_dc);
            _dc = new DataColumn("__Hide", typeof(int)); _dc.AllowDBNull = false; _dc.DefaultValue = 0; m_dt.Columns.Add(_dc);

            //弹出可选项要去掉刚已选但未提交的数量
            if (this.m_detail != null)
            {
                DataRow[] drs;
                foreach (DataRow dr in this.m_detail.Rows)
                {
                    //if (dr.RowState == DataRowState.Added)
                   // {
                    drs = this.m_dt.Select("EmployeeID=" + dr["EmployeeID"].ToString() + "");
                    if (drs.Length > 0)
                    {
                        drs[0]["__Hide"] = 1;
                    }
                   // }
                }
            }
            this.m_dt.AcceptChanges();

            this.cmbItemType.Items.Clear();
            this.cmbItemType.Items.Add("");
            String str = "";
            foreach (DataRow dr in m_dt.Rows)
            {
                str = dr["DepartmentName"].ToString();
                if (!this.cmbItemType.Items.Contains(str))
                    this.cmbItemType.Items.Add(str);
            }
            ViewData();
        }

        private void ckbAll_Click(object sender, EventArgs e)
        {
            CheckBox _ckb = sender as CheckBox;
            foreach (DataGridViewRow _vr in this.dgv.Rows)
            {
                (_vr.DataBoundItem as DataRowView).Row["Select"] = _ckb.Checked;
            }
        }

        private void cmbPubMNo_TextChanged(object sender, EventArgs e)
        {
            ViewData();
        }

        void ViewData()
        {
            string Sql = " __Hide=0 ";
            if (this.cmbItemType.Text.Trim() != "")
                Sql += " And DepartmentName='" + this.cmbItemType.Text.Trim() + "'";
            //if (this.txtZJM.Text.Trim() != "")
            //    Sql += " And ZJM Like '%" + this.txtZJM.Text.Trim() + "%'";
            if (this.txtEmpName.Text.Trim() != "")
                Sql += " And EmpName Like '%" + this.txtEmpName.Text.Trim() + "%'";

            DataView dv = this.m_dt.DefaultView;
            dv.RowFilter = Sql;
            this.dgv.DataSource = dv;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            bool bZero = false;
            bool bSelected = false;
            DataRow dr;
            //至少选择一项
            foreach (DataGridViewRow _vr in this.dgv.Rows)
            {
                dr = (_vr.DataBoundItem as DataRowView).Row;
                if (Convert.ToBoolean(dr["Select"]))
                {
                    bSelected = true; // 正常，至少选了一项
                   
                }
            }
            if (!bSelected)
            {
                MessageBox.Show("请至少选择一个员工.", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
           
            this.DialogResult = DialogResult.OK;
        }

        private void butCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel ;
            this.Close();
        }


    }
}
